package com.zh.flowable.domain.dto;

import com.zh.flowable.domain.flow.service.HisProcessInstDetail;
import com.zh.flowable.domain.flow.service.ProcessInstDetail;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;

import static com.zh.mycommon.viewdata.ToWrapper.foreach;

/**
 * @author
 * @date 2025/8/21
 */
public class ProcessInstWrapper {

    public static ProcessInstDTO convertDTO(ProcessInstDetail entity) {
        var instance = entity.getInstance();

        var dto = new ProcessInstDTO();

        dto.setId(instance.getId());

        dto.setParentId(instance.getParentId());

        dto.setName(instance.getName());

        dto.setProcessDefinitionId(instance.getProcessDefinitionId());
        dto.setProcessDefinitionKey(instance.getProcessDefinitionKey());
        dto.setProcessDefinitionName(instance.getProcessDefinitionName());

        dto.setBusinessKey(instance.getBusinessKey());
        dto.setBusinessStatus(instance.getBusinessStatus());

        dto.setStartUserId(instance.getStartUserId());
        dto.setStartTime(instance.getStartTime());

        dto.setActivityId(instance.getActivityId());

        dto.setDeploymentId(instance.getDeploymentId());

        dto.setDescription(instance.getDescription());

        dto.setTenantId(instance.getTenantId());

        //-----------

        dto.setTasks(foreach(entity.getTasks(), TaskWrapper::convertDTO));
        return dto;
    }

    //HisProcessInstDetail
    public static ProcessInstDTO convertDTO(HisProcessInstDetail entity) {
        var instance = entity.getInstance();

        var dto = new ProcessInstDTO();

        dto.setId(instance.getId());

//        dto.setParentId(instance.getParentId());

        dto.setName(instance.getName());

        dto.setProcessDefinitionId(instance.getProcessDefinitionId());
        dto.setProcessDefinitionKey(instance.getProcessDefinitionKey());
        dto.setProcessDefinitionName(instance.getProcessDefinitionName());

        dto.setBusinessKey(instance.getBusinessKey());
        dto.setBusinessStatus(instance.getBusinessStatus());

        dto.setStartUserId(instance.getStartUserId());
        dto.setStartTime(instance.getStartTime());

        dto.setStartActivityId(instance.getStartActivityId());
        dto.setEndActivityId(instance.getEndActivityId());

        dto.setDeploymentId(instance.getDeploymentId());

        dto.setDescription(instance.getDescription());

        dto.setTenantId(instance.getTenantId());

        //-----------

        dto.setTasks(foreach(entity.getTasks(), TaskWrapper::convertDTO));
        return dto;
    }
}
